package com.lagou.work;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RpcConsumer {

    // 1.创建一个线程池对象  -- 它要处理我们自定义事件
    private static ExecutorService executorService =
            Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    // 2.声明一个自定义事件处理器  UserClientHandler
    private static UserClientHandler userClientHandler;

    // 3.编写方法，初始化客户端 （创建连接池、bootStrap、设置bootStrap、连接服务器）
    public static void initClient(String address, Integer port) throws InterruptedException {
        // 1) 初始化 userClientHandler
        userClientHandler = new UserClientHandler();

        // 2) 创建连接池对象
        EventLoopGroup group = new NioEventLoopGroup();

        // 3) 创建客户端的引导对象
        Bootstrap bootstrap = new Bootstrap();

        // 4) 配置启动引导对象
        bootstrap.group(group)
            // 设置通道为NIO
            .channel(NioSocketChannel.class)
            // 设置请求协议为TCP
            .option(ChannelOption.TCP_NODELAY, true)
            // 监听channel 并初始化
            .handler(new ChannelInitializer<SocketChannel>() {
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    // 获取 ChannelPipeline
                    ChannelPipeline pipeline = socketChannel.pipeline();
                    // 设置编码
//                    pipeline.addLast(new StringEncoder());
                    pipeline.addLast(new StringDecoder());
                    pipeline.addLast(new RpcEncoder(RpcRequest.class, new JSONSerializer()));

                    // 添加自定义事件处理器
                    pipeline.addLast(userClientHandler);
                }
            });

        // 5) 连接服务端
        bootstrap.connect(address, port).sync();
        System.out.println("客户端已启动...");
    }

    // 4.编写一个方法，使用JDK的动态代理创建对象
    public static Object createProxy(Class<?> serviceClass, String ip, Integer port) {
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{serviceClass}, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 1.初始化客户端client
                if (userClientHandler == null) {
                    initClient(ip, port);
                }

                /*// 2.给 userClientHandler 设置param参数
                userClientHandler.setParam(providerParam + args[0]);*/

                String className = method.getDeclaringClass().getName(); // className
                String methodName = method.getName(); // methodName
                Class<?>[] parameterTypes = method.getParameterTypes(); // parameterTypes
                Object[] parameters = args; // Parameters

                RpcRequest rpcRequest = new RpcRequest();
                rpcRequest.setClassName(className);
                rpcRequest.setMethodName(methodName);
                rpcRequest.setParameterTypes(parameterTypes);
                rpcRequest.setParameters(parameters);

                userClientHandler.setParam(rpcRequest);

                // 3.使用线程池，开启一个线程处理call()写操作，并返回结果。
                Object o = executorService.submit(userClientHandler).get();

                // 4.return 结果
                return o;
            }
        });
    }

}
